package com.oceansoft.mobile.econsole.modules.dcn.plugin;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.oceansoft.mobile.econsole.common.constant.Constant;
import com.oceansoft.mobile.econsole.common.util.HttpClientUtil;
import com.oceansoft.mobile.econsole.modules.dcn.entity.Command;
import com.oceansoft.mobile.econsole.modules.dcn.entity.RespCmd;
import com.oceansoft.mobile.econsole.modules.dcn.processor.IProcessor;
import com.oceansoft.mobile.econsole.modules.jlapi.entity.AppMessage;
import com.oceansoft.mobile.econsole.modules.jlapi.entity.ExpireWarnQueryEntity;
import com.oceansoft.mobile.econsole.modules.jlapi.entity.InQueryServiceInfo;
import com.oceansoft.mobile.econsole.modules.jlapi.entity.WechatMessage;
import com.oceansoft.mobile.econsole.modules.member.entity.WtMember;
import com.oceansoft.mobile.econsole.modules.member.persistence.IWtMemberMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Objects;

/**
 * ClassName: ExpireNoticePlugin
 * Desc: 到期提醒插件
 * Date： 2016/11/16
 * Created：shaom
 */
@Component
public abstract class AbstractExpireNoticePlugin implements IProcessor {

    private static final Logger logger = LoggerFactory.getLogger(AbstractExpireNoticePlugin.class);

    private static final String MSG_APP_URL = "http://gawx.daqing.gov.cn/web/api/v1/msg/app/";
    private static final String MSG_WECHAT_URL = "http://gawx.daqing.gov.cn/web/api/v1/msg/wx";


    @Resource
    private IWtMemberMapper wtMemberMapper;

    @Override
    public void dealData(Command command) {
        sendMsg(command);
    }

    protected void sendMsg(Command command) {
        RespCmd respCmd = (RespCmd) command;
        logger.info("+++++++++++++ 发送短信 START +++++++++++++++++++");

        String type = "";
        String content = "";
        String action = respCmd.getAction();// 判断显示推送信息
        ExpireWarnQueryEntity entity = JSON.parseObject(respCmd.getResult(), ExpireWarnQueryEntity.class);

        WtMember wtMember = wtMemberMapper.getInfoByUserName(entity.getMobile());
        if (!Objects.isNull(wtMember) && !Strings.isNullOrEmpty(wtMember.getWeixinid())) {
            entity.setOpenId(wtMember.getWeixinid());
        }

        if (Objects.equals(InQueryServiceInfo.API_011, action)) {
            type = "身份证到期提醒";
            content = String.format(Constant.EXPIRE_WARN_MSG_IC, entity.getExpireDate());
        } else if (Objects.equals(InQueryServiceInfo.API_012, action)) {
            type = "驾驶证到期提醒";
            content = String.format(Constant.EXPIRE_WARN_MSG_VL, entity.getExpireDate());
        } else if (Objects.equals(InQueryServiceInfo.API_013, action)) {
            type = "年检到期提醒";
            content = String.format(Constant.EXPIRE_WARN_MSG_AC, entity.getHphm(), entity.getExpireDate());
        } else if (Objects.equals(InQueryServiceInfo.API_014, action)) {
            type = "违章提醒";
            content = String.format(Constant.EXPIRE_WARN_MSG_VV, entity.getHphm(), entity.getWfsj(), entity.getWfdz());
        }

        // APP
        AppMessage appMessage = new AppMessage();
        appMessage.setReceiver(entity.getMobile());// 前端商量
        appMessage.setMsgType(AppMessage.MessageType.VEHICLE_BIND);
        appMessage.setTitle(type);
        appMessage.setContent(content);
        HttpClientUtil.doPostString(MSG_APP_URL, appMessage);

        // 微信
        String openId = entity.getOpenId();
        if (!Strings.isNullOrEmpty(openId)) {
            WechatMessage wechatMessage = new WechatMessage();
            wechatMessage.setOpenId(openId);
            wechatMessage.setTempId(WechatMessage.WECHAT_TEMPLATEID);

            HashMap<String, String[]> keyMap = new HashMap<>();
            keyMap.put("first", new String[]{content, "#173177"});
            keyMap.put("keyword1", new String[]{type, "#173177"});
            keyMap.put("keyword2", new String[]{entity.getExpireDate(), "#173177"});
            wechatMessage.setParams(keyMap);
            HttpClientUtil.doPostString(MSG_WECHAT_URL, wechatMessage);
        }

        logger.info("+++++++++++++ 发送短信 END +++++++++++++++++++");

    }

}
